package com.sc.nft.admin.controller;


import com.sc.nft.admin.request.*;
import com.sc.nft.annotation.SysMemberLogin;
import com.sc.nft.entity.vo.AdminDaoEcologyPointsVO;
import com.sc.nft.entity.vo.AdminManagementRecordPageVO;
import com.sc.nft.entity.vo.ecology.AdminEcologyPointsUserPageDetailsVO;
import com.sc.nft.enums.RedisKeyEnum;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.DaoEcologyPointsService;
import com.sc.nft.sup.ErrorCode;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.concurrent.TimeUnit;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/admin/dao/ecology/points")
@Api(tags = "后台用户燃料管理")
public class AdminDaoEcologyPointsDetailController {

    private final DaoEcologyPointsService daoEcologyPointsService;
    private final Redisson redisson;

    //EcologyPointsUserPage
    @PostMapping(value = "/getPage", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "分页获取DAO燃料列表", notes = "分页获取DAO燃料列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<AdminDaoEcologyPointsVO> getPage(@Valid @RequestBody DaoEcologyPointsRequest request) {
        return PageResult.success(daoEcologyPointsService.getPage(request.getDaoName(),request.getPageNo(),request.getPageSize()));
    }

    @PostMapping(value = "/editDaoPoints", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "编辑DAO生态燃料", notes = "编辑用户生态燃料", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result editDaoPoints(@Valid @RequestBody AdminEditDaoPointsRequest request) {
        RLock lock = redisson.getLock(RedisKeyEnum.EDIT_DAO_POINTS_LOCK.getKey() + request.getDaoId());
        try {
            boolean res = lock.tryLock(3, TimeUnit.SECONDS);
            if (res) {
                daoEcologyPointsService.editDaoPoints(request.getDaoId(),request.getIsAdd(),request.getAmount(),request.getRemark());
            }else {
                return Result.fail(ErrorCode.LATER_TRY);
            }
        } catch (InterruptedException e) {
            throw new GlobalRunTimeException("系统超时");
        } finally {
            lock.unlock();
        }
        return Result.success();
    }

    @PostMapping(value = "/managementRecordPage", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "管理记录", notes = "管理记录", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<AdminManagementRecordPageVO> managementRecordPage(@Valid @RequestBody ManagementRecordPageRequest request) {
        return PageResult.success(daoEcologyPointsService.managementRecordPage(request.getDaoId(),request.getIsAdd(),request.getStartTime(),request.getEndTime(),request.getPageNo(),request.getPageSize()));
    }



}
